package com.ruoyi.business.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.ruoyi.business.domain.BusinessOrgRelationRel;
import com.ruoyi.business.service.IBusinessOrgRelationRelService;
import com.ruoyi.business.service.IBusinessTeacherService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.BusinessTeacher;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 教师信息Controller
 * 
 * @author ruoyi
 * @date 2022-11-23
 */
@Controller
@RequestMapping("/business/teacher")
public class BusinessTeacherController extends BaseController
{
    private String prefix = "business/teacher";

    @Autowired
    private IBusinessTeacherService businessTeacherService;

    @Autowired
    private IBusinessOrgRelationRelService orgRelationRelService;

    @RequiresPermissions("business:teacher:view")
    @GetMapping()
    public String teacher()
    {
        return prefix + "/teacher";
    }

    /**
     * 查询教师信息列表
     */
//    @RequiresPermissions("business:teacher:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BusinessTeacher businessTeacher)
    {
        startPage();
        List<BusinessTeacher> list = businessTeacherService.selectBusinessTeacherList(businessTeacher);
        return getDataTable(list);
    }

    /**
     * 导出教师信息列表
     */
//    @RequiresPermissions("business:teacher:export")
    @Log(title = "教师信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BusinessTeacher businessTeacher)
    {
        List<BusinessTeacher> list = businessTeacherService.selectBusinessTeacherList(businessTeacher);
        ExcelUtil<BusinessTeacher> util = new ExcelUtil<BusinessTeacher>(BusinessTeacher.class);
        return util.exportExcel(list, "教师信息数据");
    }

    /**
     * 新增教师信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存教师信息
     */
//    @RequiresPermissions("business:teacher:add")
    @Log(title = "教师信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam(value = "teacherImgFile", required = false) MultipartFile teacherImgFile,
                              BusinessTeacher businessTeacher) throws IOException {
        if(teacherImgFile != null){
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileOrgImgName = FileUploadUtils.upload(filePath, teacherImgFile);
            businessTeacher.setTeacherImg(fileOrgImgName);
        }
        businessTeacherService.insertBusinessTeacher(businessTeacher);

        //删除系统新增的type=0 与教师关联关系 系统默认为1
        Long orgId = 1L;
        BusinessOrgRelationRel businessTeacherRel = new BusinessOrgRelationRel();
        businessTeacherRel.setRelationId(businessTeacher.getTeacherId());
        businessTeacherRel.setOrgId(orgId);
        businessTeacherRel.setType("1");
        businessTeacherRel.setTeacherType("0");

        orgRelationRelService.insertBusinessOrgRelationRel(businessTeacherRel);

        return toAjax(true);
    }

    /**
     * 修改教师信息
     */
//    @RequiresPermissions("business:teacher:edit")
    @GetMapping("/edit/{teacherId}")
    public String edit(@PathVariable("teacherId") Long teacherId, ModelMap mmap)
    {
        BusinessTeacher businessTeacher = businessTeacherService.selectBusinessTeacherByTeacherId(teacherId);
        mmap.put("businessTeacher", businessTeacher);
        return prefix + "/edit";
    }

    /**
     * 修改保存教师信息
     */
//    @RequiresPermissions("business:teacher:edit")
    @Log(title = "教师信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestParam(value = "teacherImg", required = false) MultipartFile teacherImg,
                               BusinessTeacher businessTeacher) throws IOException {
        if(teacherImg != null){
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileOrgImgName = FileUploadUtils.upload(filePath, teacherImg);
            businessTeacher.setTeacherImg(fileOrgImgName);
        }
        return toAjax(businessTeacherService.updateBusinessTeacher(businessTeacher));
    }

    /**
     * 删除教师信息
     */
//    @RequiresPermissions("business:teacher:remove")
    @Log(title = "教师信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        businessTeacherService.deleteBusinessTeacherByTeacherIds(ids);
        //删除系统新增的type=0 与教师关联关系 系统默认为1
        String orgId = "1";
        List<String> teacherIds  = Arrays.asList(ids.split(",").clone());
        orgRelationRelService.deleteBusinessOrgRelationRelByTeacherIdAndOrgId(teacherIds, orgId);

        return toAjax(true);
    }
}
